/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.Board;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 *
 * @author Wink
 */
@Stateless
@Path("boards")
public class BoardsFacadeREST extends AbstractFacade<Board> {
    @PersistenceContext(unitName = "HashArea-warPU")
    private EntityManager em;

    public BoardsFacadeREST() {
        super(Board.class);
    }

    @POST
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public Board createBoard(Board entity) {
        System.out.println("creating board");
        em.persist(entity);
        em.flush();
        return entity;
    }

//    @PUT
//    @Override
//    @Consumes({"application/json", "application/xml"})
//    public void edit(Board entity) {
//        super.edit(entity);
//    }

    @DELETE
    public void remove(@QueryParam("id") Long id) {
        super.remove(super.find(id));
    }

    @GET
    @Produces({"application/json", "application/xml"})
    public List<Board> findAll(@QueryParam("n") Double north,
                                @QueryParam("s") Double south,
                                @QueryParam("e") Double east,
                                @QueryParam("w") Double west,
                                @QueryParam("zoom") Integer zoom,
                                @QueryParam("revealed") String revealed) {
        if (north != null && south != null 
                && east != null && west != null
                && zoom != null) {
            //get the list of already obtained boards
            List<String> revealedList = Arrays.asList(revealed.split("[,]"));
            System.out.println("revealed = " + revealed);
            ArrayList<Board> lb = new ArrayList<Board>();
            while (lb.size() < 10 && zoom < 20) {
                Query findBoards = em.createNamedQuery("Board.find")
                    .setParameter("north", north)
                    .setParameter("south", south)
                    .setParameter("east", east)
                    .setParameter("west", west)
                    .setParameter("zoom", zoom)
                    .setParameter("revealed", revealedList);
                lb.addAll(findBoards
                    .getResultList());
                zoom++;
            }
            System.out.println("Returning " + lb.size() + " boards.");
            return lb;
        } else {
           return new ArrayList<Board>(); //empty response
        }
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
